import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_app_info.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/routes/account/view/xindong_yinsi_view.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/xindong_countdown_button.dart';
import 'package:xindong/widgets/xindong_number_text_field.dart';

import 'controller/xindong_login_ctr.dart';

class XinDongLoginYeMian extends StatefulWidget {
  const XinDongLoginYeMian({super.key});

  @override
  State<XinDongLoginYeMian> createState() => _XinDongLoginYeMianState();
}

class _XinDongLoginYeMianState extends State<XinDongLoginYeMian> {
  @override
  void initState() {
    super.initState();
    XinDongLoginCtr.to.setup();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }

  @override
  Widget build(BuildContext context) {
    final controller = XinDongLoginCtr.to;
    return GestureDetector(
      onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
      // onTap: () => XinDongPageSingleton.instance.pushYeMian(const XinDongRegisterYeMian()),
      behavior: HitTestBehavior.opaque,
      child: Scaffold(
        body: Stack(
          alignment: Alignment.center,
          children: [
            XinDongAppBar(
              '',
              leading: const SizedBox(),
              backgroundColor: DZColors.backgroundColor,
            ),
            Center(
              child: Container(
                width: 300,
                alignment: Alignment.center,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.only(left: 8, bottom: 30),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            XinDongAppDeviceInfo.instance.showAppName,
                            style: TextStyle(
                                color: DZColors.hexColor(0x323232), fontSize: 20, fontWeight: FontWeight.w500),
                          ),
                          const SizedBox(height: 10),
                          Text(
                            "寻觅爱的气息",
                            style: TextStyle(color: DZColors.hexColor(0x323232), fontSize: 16),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 48,
                      child: XinDongNumberTextField(
                        hintText: '请输入手机号',
                        controller: controller.phoneController,
                        keyboardType: TextInputType.phone,
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(11)],
                        prefixIcon: Container(
                          height: double.infinity,
                          margin: const EdgeInsets.only(left: 20),
                          constraints: const BoxConstraints(minWidth: 50.0),
                          child: const Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                "+86 |",
                                textAlign: TextAlign.center,
                                style: TextStyle(fontSize: 16, color: DZColors.placeholderColor, height: 1.2),
                              ),
                            ],
                          ),
                        ),
                        onChanged: (text) => controller.onAutoLogin(),
                      ),
                    ),
                    const SizedBox(height: 16),
                    SizedBox(
                      height: 48,
                      child: XinDongNumberTextField(
                        hintText: '请输入验证码',
                        focusNode: controller.codeFocusNode,
                        controller: controller.codeController,
                        keyboardType: TextInputType.number,
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(4)],
                        suffixIcon: Padding(
                          padding: const EdgeInsets.only(right: 20),
                          child: XinDongCountdownButton(onTapSend: () {
                            controller.onGetCode();
                          }),
                        ),
                        onChanged: (text) => controller.onAutoLogin(),
                      ),
                    ),
                    const SizedBox(height: 20),
                    SizedBox(
                      height: kButtonHeight,
                      child: Obx(
                        () => XinDongCommonButton(
                          "登录/注册",
                          onTap: controller.enableLoginButton.value ? () => controller.requestLogin() : null,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    const XinDongAgreementView(),
                  ],
                ),
              ),
            ),
            Positioned(
              top: statusBarHeight,
              right: 16,
              child: TextButton(
                onPressed: () => controller.toPasswordLoginYeMian(),
                child: const Text(
                  "密码登录",
                  style: TextStyle(fontSize: 15, color: DZColors.contentColor),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
